import 'virtual_offset.dart';

// little class representing a chunk in the index
class Chunk implements Comparable<Chunk> {
  VirtualOffset minv;
  VirtualOffset maxv;
  num bin;
  num _fetchedSize;
  List<int> buffer;

  /// @param {VirtualOffset} minv
  /// @param {VirtualOffset} maxv
  /// @param {number} bin
  /// @param {number} [fetchedSize]
  Chunk(this.minv, this.maxv, this.bin, [num fetchedSize, this.buffer]) {
    this._fetchedSize = fetchedSize;
  }

  toUniqueString() {
    return '${this.minv}..${this.maxv} (bin ${this.bin}, fetchedSize ${this.fetchedSize()})';
  }

  toString() {
    return this.toUniqueString();
  }

  @override
  int compareTo(Chunk other) {
    int r = this.minv.compareTo(other.minv);
    int max = this.maxv.compareTo(other.maxv);
    return r == 0 ? (max == 0 ? bin - other.bin : max) : r;
  }

  num fetchedSize() {
    if (this._fetchedSize != null) return this._fetchedSize;
    return this.maxv.blockPosition + (1 << 16) - this.minv.blockPosition;
  }
}
